/**
 * 
 */
package com.thomsonreuters.elektronunified;


import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * Data object representing a response from the EU system to a request for data. 
 * <P>
 * Every response will include the user specified identifier (if set in the original request), the item requested, 
 * the status of the item and a description of the status. 
 * <P>
 * If there is data (as FID, value pairs) this is also contained within the response.
 * <P>
 * ResponseItems are returned to the client caller through the <I><B>onMarketData</I></B> call-back.
 */ 

public class ResponseItem 
{
	private boolean debugExtend = false;
	
	private String userIdentifier;
	private String responseItemName;
	
	private Boolean responseItemStatusOK		= false;
	private Boolean responseItemStatusOpen		= false;
	private Boolean responseItemStatusRecovering= false;
	
	private String responseItemType;
	private String responseItemCode;
	private String responseItemState;
	
	Comparator<String> compareFIDs = new Comparator<String>()
	{
		@Override 
		public int compare(String fid1, String fid2) 
		{
			Integer n1 = Integer.parseInt(fid1);
			Integer n2 = Integer.parseInt(fid2);
			return n1.compareTo(n2);	 
		}
	};
	
	private Map<String, String> fidVals = new TreeMap<String, String>(compareFIDs);

	void setExtend ( Boolean debugExtend ) { this.debugExtend = debugExtend; }
	
	void setUserIdentifier(String userIdentifier) { this.userIdentifier = userIdentifier; }
	
	/**
	 * Gets the user specified identifier for this response item.
	 * 
	 */
	public String getUserIdentifier() { return this.userIdentifier; }
	
	/**
	 * Gets the item/instrument name (RIC) for this response item.
	 * 
	 */
	public String getResponseItemName() { return this.responseItemName; }
	
	protected void setResponseItemName(String responseItemName) { this.responseItemName = responseItemName; }

	/**
	 * is the response item status OK.
	 * 
	 */
	public Boolean isResponseItemStatusOK() { return this.responseItemStatusOK; }
	
	void setResponseItemStatusOK(Boolean responseItemStatusOK) { this.responseItemStatusOK = responseItemStatusOK; }
	
	/**
	 * is the response item still open
	 * 
	 */
	public Boolean isResponseItemStatusOpen() { return this.responseItemStatusOpen; }
	
	void setResponseItemStatusOpen(Boolean responseItemStatusOpen) { this.responseItemStatusOpen = responseItemStatusOpen; }

	/**
	 * Is the response item in a recovering state
	 *  
	 */
	public Boolean isResponseItemStatusRecovering() { return this.responseItemStatusRecovering; }
	
	void setResponseItemStatusRecovering(Boolean responseItemStatusRecovering) { this.responseItemStatusRecovering = responseItemStatusRecovering; }
	
	
	public String getResponseItemType() { return this.responseItemType; }
	
	void setResponseItemType(String responseItemType) { this.responseItemType = responseItemType; }
	
	public String getResponseItemCode() { return this.responseItemCode; }
	
	void setResponseItemCode(String responseItemCode) { this.responseItemCode = responseItemCode; }
	
	public String getResponseItemState() { return this.responseItemState; }
	
	void setResponseItemState(String responseItemState) { this.responseItemState = responseItemState; }
	
	protected void putFidsAndValues(String fid, String Value) { this.fidVals.put(fid, Value);	}
	
	/**
	 * Gets the response item map of the fields (as Field Identifiers) and values.
	 * 
	 */
	public Map<String, String> getFidsAndValues() {	return fidVals; }
	
	/**
	 * Returns a String representation of the ResponseItem.
	 * <P>
	 * The representation consists of:
	 * <P>
	 * <UL><LI>
	 * A header line containing the user identifier, the item name, item status, and item status detail.
	 * <LI>
	 * Formatted strings of any data pay-load as Field identifiers (FIDs) and values, ordered by FID.
	 * 
	 * 
	 */		
	public String toString()
	{
		  int i = 0;
		  StringBuilder sb = new StringBuilder();
		  
		  sb.append("Response: \n");
		  sb.append("User Identifier: '");
		  sb.append(this.userIdentifier);
		  sb.append('\'');
		  sb.append(" RIC='");
		  sb.append(this.responseItemName);
		  sb.append('\'');
		  sb.append(" OK='");
		  sb.append(this.responseItemStatusOK.toString());
		  sb.append('\'');
		  sb.append(" OPEN='");
		  sb.append(this.responseItemStatusOpen.toString());
		  sb.append('\'');
		  sb.append(" RECOVERY='");
		  sb.append(this.responseItemStatusRecovering.toString());
		  sb.append('\'');
		  
		  if ( debugExtend ) {
			  sb.append(" TYPE='");
			  sb.append(this.responseItemType);
			  sb.append('\'');
			  sb.append(" STATE:'");
			  sb.append(this.responseItemState);
			  sb.append('\'');
			  sb.append(" CODE:'");
			  sb.append(this.responseItemCode);
			  sb.append('\'');
		  }
		  
		  for (Entry<String, String> FidVal : this.fidVals.entrySet()) 
		  {
			  if ( i == 0 ) 
			  {
				  sb.append("\n");
			  }
			  
			  i++;
			  sb.append("FID:" + String.format("%-5s", FidVal.getKey()) + "=" + FidVal.getValue() );
			  
			  if (i%5 == 0) 
			  {
				  sb.append("\n");
			  }			  
		  }

		  return sb.toString();
	}
}
